<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="请输入查询内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleAdd">
        添加分组
      </el-button>
    </div>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top: 10px;"
    >
      <el-table-column
        prop="id"
        label="序号"
        min-width="20%"
      />
      <el-table-column
        prop="name"
        label="分组名"
        min-width="30%"
      />
      <el-table-column label="操作" min-width="20%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>

          <el-popconfirm
            :title="'确定删除分组 '+scope.row.name+' 吗?'"
            style="margin-left: 10px;"
            @onConfirm="handleDel(scope.$index, scope.row)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="danger"
            >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="创建设备分组" :visible.sync="addDeviceGroupFormVisible">
      <el-form ref="addDeviceGroupForm" :rules="addDeviceGroupFormRules" :model="addDeviceGroupForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="分组名" prop="name">
          <el-input v-model="addDeviceGroupForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDeviceGroupFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="add()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改设备分组" :visible.sync="editDeviceGroupFormVisible">
      <el-form ref="editDeviceGroupForm" :rules="editDeviceGroupFormRules" :model="editDeviceGroupForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="分组名" prop="name">
          <el-input v-model="editDeviceGroupForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDeviceGroupFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="edit()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { Message } from 'element-ui'
import { getDeviceGroupList, addDeviceGroup, editDeviceGroup, delDeviceGroup } from '../../api/device.js'
export default {
  data() {
    return {
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        search: ''
      },
      addDeviceGroupFormVisible: false,
      addDeviceGroupFormRules: {
        name: [{ required: true, message: '分组名必须填写', trigger: 'blur' }]
      },
      addDeviceGroupForm: {
        name: ''
      },
      editDeviceGroupFormVisible: false,
      editDeviceGroupFormRules: {
        name: [{ required: true, message: '分组名必须填写', trigger: 'blur' }]
      },
      editDeviceGroupForm: {
        id: '',
        name: ''
      }
    }
  },
  created() {
    this.getDeviceGroupList()
  },
  methods: {
    getDeviceGroupList() {
      this.listLoading = true
      getDeviceGroupList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
      })
    },
    resetRow() {
      this.row = {
        name: ''
      }
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getDeviceGroupList()
    },
    handleAdd() {
      this.resetRow()
      this.addDeviceGroupFormVisible = true
      this.$nextTick(() => {
        this.$refs['addDeviceGroupForm'].clearValidate()
      })
    },
    handleEdit(index, row) {
      this.editDeviceGroupForm.id = row.id
      this.editDeviceGroupForm.name = row.name
      this.editDeviceGroupFormVisible = true
    },
    handleDel(index, row) {
      this.del(row.id)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getDeviceGroupList()
    },
    add() {
      this.$refs['addDeviceGroupForm'].validate((valid) => {
        if (valid) {
          addDeviceGroup(this.addDeviceGroupForm).then(response => {
            if (response.code === 1) {
              Message({
                message: response.msg,
                type: 'success',
                duration: 5 * 1000
              })
              this.addDeviceGroupFormVisible = false
              this.getDeviceGroupList()
            }
          })
        }
      })
    },
    del(id) {
      delDeviceGroup({ id: id }).then(response => {
        if (response.code === 1) {
          Message({
            message: response.msg,
            type: 'success',
            duration: 5 * 1000
          })
          this.addDeviceGroupFormVisible = false
          this.getDeviceGroupList()
        }
      })
    },
    edit() {
      editDeviceGroup(this.editDeviceGroupForm).then(response => {
        if (response.code === 1) {
          Message({
            message: response.msg,
            type: 'success',
            duration: 5 * 1000
          })
          this.editDeviceGroupFormVisible = false
          this.getDeviceGroupList()
        }
      })
    }

  }
}
</script>
